package bounceBall;

import java.util.ArrayList;
import javafx.animation.Animation;
import javafx.animation.ParallelTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

// 弹球
public class Main extends Application {
	private Pane root = new Pane();
	private BounceBall firstBall;		// 弹球1
	private BounceBall secondBall;		// 弹球2
	private BounceBall thirdBall;		// 弹球3
	private BounceBall fourthBall;
	private ParallelTransition pt;
	public static ArrayList<BounceBall> balls = new ArrayList<>();		// 存放弹球的数组
	
	public static void main(String[] args) {
		launch(args);
	}
	
	@Override
	public void start(Stage stage) throws Exception {
		
		root.setStyle("-fx-background-color: linear-gradient(to right, #A1FFCE, #FAFFD1)");    
		
		firstBall = new BounceBall(40, 40, 25, Color.RED);
		secondBall = new BounceBall(400, 20, 20, Color.ORANGE);
		thirdBall = new BounceBall(40, 300, 30, Color.DODGERBLUE);
		fourthBall = new BounceBall(300, 300, 15, Color.LIMEGREEN);
		balls.add(firstBall);
		balls.add(secondBall);
		balls.add(thirdBall);
		balls.add(fourthBall);
		
		root.getChildren().addAll(firstBall.getBall(), secondBall.getBall(), 
							thirdBall.getBall(), fourthBall.getBall());
		
		Scene scene = new Scene(root, 600, 400);
		stage.setScene(scene);
		stage.setTitle("弹球");
		stage.show();
		
		// 动画
		pt = new ParallelTransition(firstBall.getTimeline(), secondBall.getTimeline(), 
					thirdBall.getTimeline(), fourthBall.getTimeline());
		pt.play();		// 各动画不用再单独play
		
		// 事件
		root.setOnMouseClicked(e -> controlStatus() );
	}
	
	// 控制动画状态
	public void controlStatus() {
		if(pt.getStatus() == Animation.Status.PAUSED) {
			pt.play();
		} else{
			pt.pause();
		}
	}
}
